package com.edu.service.impl;

import java.util.List;

import com.edu.dao.UserBackgroundDao;
import com.edu.dao.impl.UserBackgroundDaoImpl;
import com.edu.entity.PageBean;
import com.edu.entity.User;
import com.edu.entity.shopDingdong;
import com.edu.service.UserBackgroundService;

public class UserBackgroundServiceImpl implements UserBackgroundService {
	UserBackgroundDao userBackDao=new UserBackgroundDaoImpl();
	@Override
	public PageBean<User> queryUser(int currentPageNum, int pageSize,String tableName) {
		// TODO Auto-generated method stub
		PageBean<User> pageBean = new PageBean<User>();
		//总条数
		int count = userBackDao.count(tableName);
		System.out.println(count);
		//总页数
		int pageNum=count%pageSize==0?count/pageSize:count/pageSize+1;
		currentPageNum=currentPageNum<1?1:currentPageNum;//上一页
		currentPageNum=currentPageNum>pageNum?pageNum:currentPageNum;//上一页
		//显示数量
		int num = (currentPageNum-1)*pageSize;
		//显示数据
		List<User> data = userBackDao.queryUser(num, pageSize);
		pageBean.setCount(count);
		pageBean.setPageSize(pageSize);
		pageBean.setPageNum(pageNum);
		pageBean.setCurrentPageNum(currentPageNum);
		pageBean.setData(data);
		return pageBean;
	}

	@Override
	public int count(String tableName) {
		// TODO Auto-generated method stub
		int countNum = userBackDao.count(tableName);
		if(countNum == -1) {
			System.out.println("sql有误！");
		}
		return countNum;
	}
	@Override
	public boolean addUser(User user) {
		// TODO Auto-generated method stub
		int rowNum=userBackDao.addUser(user);
		if(rowNum==1) {
			return true;
		}
		return false;
	}
	@Override
	public boolean deleteId(int userId,String tableId,String tableName) {
		// TODO Auto-generated method stub
		int rowNum = userBackDao.deleteById(userId,tableId,tableName);
		if(rowNum == 1) {
			return true;		
		}
		return false;
	}
	@Override
	public boolean updateById(User user) {
		// TODO Auto-generated method stub
//		int rowNum=userBackDao.updateById(user);
//		if(rowNum==1) {
//			return true;
//		}
		return false;
	}

	@Override
	public PageBean<shopDingdong> queryShop(int currentPageNum,int pageSize,String tableName) {
		// TODO Auto-generated method stub
		
		PageBean<shopDingdong> pageBean = new PageBean<shopDingdong>();
		//总条数
		int count = userBackDao.count(tableName);
		System.out.println(count);
		//总页数
		int pageNum=count%pageSize==0?count/pageSize:count/pageSize+1;
		currentPageNum=currentPageNum<1?1:currentPageNum;//上一页
		currentPageNum=currentPageNum>pageNum?pageNum:currentPageNum;//上一页
		//显示数量
		int num = (currentPageNum-1)*pageSize;
		//显示数据
		List<shopDingdong> data = userBackDao.queryShop(num, pageSize);
		pageBean.setCount(count);
		pageBean.setPageSize(pageSize);
		pageBean.setPageNum(pageNum);
		pageBean.setCurrentPageNum(currentPageNum);
		pageBean.setData(data);
		return pageBean;
	}
	
	@Override
	public PageBean<User> selectByUser(User user,int currentPageNum,int pageSize) {
		// TODO Auto-generated method stub
		PageBean<User> pageBean = new PageBean<User>();
		//总条数
		int count = userBackDao.count("users");
		//总页数
		int pageNum=count%pageSize==0?count/pageSize:count/pageSize+1;
		currentPageNum=currentPageNum<1?1:currentPageNum;//上一页
		currentPageNum=currentPageNum>pageNum?pageNum:currentPageNum;//下一页
		//显示数量
		int num = (currentPageNum-1)*pageSize;
		//显示数据
		List<User> data = userBackDao.selectByUser(user, num, pageSize);
		pageBean.setCount(count);
		pageBean.setPageSize(pageSize);
		pageBean.setPageNum(pageNum);
		pageBean.setCurrentPageNum(currentPageNum);
		pageBean.setData(data);
		return pageBean;
	}
	
	@Override
	public PageBean<shopDingdong> selectByShop(shopDingdong shop,int currentPageNum,int pageSize) {
		// TODO Auto-generated method stub
		PageBean<shopDingdong> pageBean = new PageBean<shopDingdong>();
		//总条数
		int count = userBackDao.count("shopdingdong");
		//总页数
		int pageNum=count%pageSize==0?count/pageSize:count/pageSize+1;
		currentPageNum=currentPageNum<1?1:currentPageNum;//上一页
		currentPageNum=currentPageNum>pageNum?pageNum:currentPageNum;//下一页
		//显示数量
		int num = (currentPageNum-1)*pageSize;
		//显示数据
		List<shopDingdong> data = userBackDao.selectByShop(shop, num, pageSize);
		pageBean.setCount(count);
		pageBean.setPageSize(pageSize);
		pageBean.setPageNum(pageNum);
		pageBean.setCurrentPageNum(currentPageNum);
		pageBean.setData(data);
		return pageBean;
	}

	@Override
	public boolean addShop(shopDingdong shop) {
		// TODO Auto-generated method stub
		int rowNum=userBackDao.addShop(shop);
		if(rowNum==1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateUserById(User user) {
		// TODO Auto-generated method stub
		int rowNum = userBackDao.updateUserById(user);
		if(rowNum == 1) {
			return true;		
		}
		return false;
	}

	@Override
	public boolean updateShopById(shopDingdong shop) {
		// TODO Auto-generated method stub
		int rowNum = userBackDao.updateShopById(shop);
		if(rowNum == 1) {
			return true;		
		}
		return false;
	}
}
